import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

chromedriver_path = 'C:\Program Files\Google\Chrome\Application\chromedriver.exe'


def get_cookie(ip, port, username, password):
    """
    使用selenium登录网页，获取cookie，供后续接口调用
    :param browser:
    :param ip:
    :param port:
    :param username:
    :param password:
    :return:
    """
    # Chromedrive存放的路径（注意Chromedrive版本需要与chrome版本一致）
    chromedriver_path = 'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
    service = Service(chromedriver_path)
    option = webdriver.ChromeOptions()
    # 不弹出chrome浏览器
    option.add_argument("headless")
    browser = webdriver.Chrome(service=service, options=option)
    browser.get("http://{}:{}/login".format(ip, port))
    time.sleep(1)
    browser.find_element(By.NAME, 'username').send_keys(username)  # 填入用户名
    time.sleep(1)
    browser.find_element(By.NAME, 'password').send_keys(password)  # 填入密码
    time.sleep(1)
    browser.find_element(By.ID, "btnSubmit").click()  # 填入密码
    time.sleep(1)

    cookies_list = browser.get_cookies()
    # print(cookies_list)
    cookie_value = cookies_list[0].get("value")
    print(cookie_value)
    time.sleep(1)
    return cookie_value


def request_station_code(ip, port, username, password, stationCode):
    """
    设置站点编号
    :param ip:
    :param port:
    :param cookie_value:
    :return:
    """
    cookie_value = get_cookie(ip, port, username, password)

    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Connection": "keep-alive",
        "Content-Length": "0",
        "Cookie": "JSESSIONID={}".format(cookie_value),
        "Host": "{}:{}".format(ip, port),
        "Origin": "http://{}:{}".format(ip, port),
        "Referer": "http://{}:{}/monitor/job".format(ip, port),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
        "X-Requested-With": "XMLHttpRequest"
    }

    try:
        body = {"id": 1,
                "stationCode": stationCode,
                "stationType": 2,
                "stationName": 11111111111,
                "address": None,
                "longitudes": None,
                "latitudes": None,
                "remark": None}  # 参数为dict形式
        print(body)
        res = requests.post("http://{}:{}/transformersubstation/transformersubstation/edit".format(ip, port),
                            headers=headers, timeout=5,
                            data=body)
    except:
        pass
    res_str = str(res.content, encoding="utf-8")
    print(res_str)


def request_refresh_cache(ip, port, username, password):
    """
    参数设置界面刷新缓存
    :param ip:
    :param port:
    :param cookie_value:
    :return:
    """
    cookie_value = get_cookie(ip, port, username, password)

    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Connection": "keep-alive",
        "Content-Length": "0",
        "Cookie": "JSESSIONID={}".format(cookie_value),
        "Host": "{}:{}".format(ip, port),
        "Origin": "http://{}:{}".format(ip, port),
        "Referer": "http://{}:{}/monitor/job".format(ip, port),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
        "X-Requested-With": "XMLHttpRequest"
    }

    try:
        res = requests.get("http://{}:{}/system/config/refreshCache".format(ip, port), headers=headers, timeout=5)
    except:
        pass
    res_str = str(res.content, encoding="utf-8")
    print(res_str)


if __name__ == '__main__':
    ip = "192.168.253.113"
    # 触发接口
    request_station_code(ip, 8082, "oetsky", "oetsky@45296", "42000002")
